package com.bskj.bms.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bskj.bms.bean.req.UserListReqVo;
import com.bskj.bms.bean.vo.AllInfoUserVo;
import com.bskj.bms.service.IBmsUserService;
import com.bskj.common.bean.model.BmsUser;
import com.bskj.common.dao.core.Page;
import com.bskj.common.enums.BmsUserTypeEnum;

@Controller
@RequestMapping("user")
public class UserController extends BaseController {
	@Autowired
	private IBmsUserService bmsUserService;

	@RequestMapping("/index")
	public ModelAndView index(HttpServletRequest req, String username, String password) throws Exception {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/user/index");
		return mv;
	}

	@RequestMapping("/openSaveOrUpdate")
	public ModelAndView openSaveOrUpdate(@RequestParam(required = false) Integer userId) throws Exception {
		ModelAndView mv = new ModelAndView();
		BmsUser bmsUser = getSessionUser();
		mv.setViewName("/user/saveOrUpdate");
		if(bmsUser != null) {
			mv.addObject("allUserType", bmsUserService.listShowUserType(bmsUser.getType()));
		}
		if (userId != null) {
			AllInfoUserVo userInfo = bmsUserService.getAllInfoUser(userId);
			mv.addObject("userInfo", userInfo);
		}
		return mv;
	}

	@ResponseBody
	@RequestMapping("list")
	public Map<String, Object> list(UserListReqVo req) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		BmsUser bmsUser = getSessionUser();
		if (bmsUser != null) {
			req.setCustId(bmsUser.getCustId());
			req.setUserType(bmsUser.getType());
			req.setShopId(bmsUser.getShopId());
		}
		Page<AllInfoUserVo> bmsUserPage = bmsUserService.pageUser(req);
		resultMap.put("data", bmsUserPage.getList());
		resultMap.put("draw", req.getDraw());
		resultMap.put("recordsTotal", bmsUserPage.getList().size());
		resultMap.put("recordsFiltered", bmsUserPage.getTotalRow());
		return resultMap;
	}
	
	@ResponseBody
	@RequestMapping("changeStatus")
	public Map<String, Object> list(Integer id,int status) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		BmsUser user = bmsUserService.get(id);
		if(user == null) {
			resultMap.put("success", false);
			resultMap.put("msg", "用户不存在");
			return resultMap;
		} else {
			if(BmsUserTypeEnum.get(status) == null) {
				resultMap.put("success", false);
				resultMap.put("msg", "请求参数不合法");
				return resultMap;
			}
			user.setStatus(status);
			user.setUpdateTime(new Date());
			bmsUserService.update(user);
			resultMap.put("success", true);
		}
		return resultMap;
	}
}
